﻿namespace ufida.u9.ait.openapi
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.Base.UOM;
    using UFIDA.U9.CBO.MFG.CostElement;
    using UFIDA.U9.CBO.Pub.Controller;
    using UFIDA.U9.CBO.QC.QCSchema;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.CBO.SCM.Supplier;
    using UFIDA.U9.ISV.Item.Proxy;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Business.Tool;
    using UFSoft.UBF.Transactions;
    using UFSoft.UBF.Util.Context;
    using UFSoft.UBF.Util.Data;
    using UFSoft.UBF.Util.DataAccess;

    /// <summary>
    /// ItemMasterSaveSV partial 
    /// </summary>	
    public partial class ItemMasterSaveSV
    {
        internal BaseStrategy Select()
        {
            return new ItemMasterSaveSVImpementStrategy();
        }
    }

    #region  implement strategy	
    /// <summary>
    /// Impement Implement
    /// 
    /// </summary>	
    internal partial class ItemMasterSaveSVImpementStrategy : BaseStrategy
    {
        public ItemMasterSaveSVImpementStrategy() { }

        public override object Do(object obj)
        {
            ItemMasterSaveSV bpObj = (ItemMasterSaveSV)obj;

            //兼容天元项目,还原之前的服务返回值,不再调用框架方法
            //return CodeUtils.EntitySave(bpObj, bpObj.Data, ItemMasterBase.CreateItem, ItemMasterBase.UpdateItem);

            UBFTransactionScope scope = null;
            bool useTrans = true;
            List<CopyOfItemMasterDTOData> dtoData = bpObj.Data;
            
            try
            {
                //启用UBF事务
                if (useTrans) scope = new UBFTransactionScope(TransactionOption.RequiresNew);

                if (dtoData == null || dtoData.Count == 0)
                    throw new Exception("传入参数为空");
                List<CommonArchiveDataDTOData> retlist = new List<CommonArchiveDataDTOData>();

                foreach (var dto in dtoData)
                {
                    ItemMasterData ret = null;
                    //查询实体
                    dto.Org = new CopyOfCommonArchiveDataDTOData() { ID = Context.LoginOrg.ID };
                    String path = DTOUtils.GetDocNoPath(dto);
                    EntityQuery query = new EntityQuery(typeof(ItemMaster).FullName);
                    System.Collections.IList list = query.FindAll(path);

                    ItemMaster item = list?.Count > 0 ? (ItemMaster)list[0] : null;
                    if (item == null)
                    {
                        //新增逻辑
                        ret = ItemMasterBase.CreateItem(dto);
                    }
                    else
                    {
                        //修改逻辑
                        ret = ItemMasterBase.UpdateItem(dto, item);
                    }
                    //返回值
                    if (ret != null) retlist.Add(new CommonArchiveDataDTOData()
                    {
                        ID=ret.ID,
                        Code = ret.Code,
                        Name = ret.Name,
                    });
                }

                //整体事务提交
                if (scope != null) scope.Commit();
                return ResultUtils.success(bpObj, dtoData, retlist);
            }
            catch (Exception ex)
            {
                //事务回滚
                if (scope != null) scope.Rollback();
                return ResultUtils.error(bpObj, dtoData, ex);
            }
            finally
            {
                //查看源码 Release()和Dispose()作用相同
                if (scope != null) scope.Release();
            }
        }
    }
    #endregion
}